package com.kehutong.payfees.service;

import com.kehutong.common.errcode.Result;
import com.kehutong.payfees.entity.ChargeFeeType;
import com.kehutong.payfees.entity.ChargeFeeTypeExt;
import com.kehutong.payfees.enums.FeesType;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.util.Enums;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.List;


@WebService("/payfees/charge/feeType")
public class BillFeeTypeServiceImpl{

    @Inject
    private JdbcSession jdbcSession;

    public Object create(JSONObject param) {
        String name = param.getString("name").trim();
        String type = param.getString("type").trim();
        ChargeFeeType c = jdbcSession.findOne(ChargeFeeType.class)
                .eq("name", name)
                .exe();
        if(Objects.nonNull(c)){
            return Result.error(214030, "收费科目名称已经存在!");
        }
        ChargeFeeType cft = Pool.newInstance(ChargeFeeType.class);
        cft.setName(name);
        cft.setFeetype(Enums.getEnum(FeesType.class, type));
        jdbcSession.insert(cft);
        JSONObject json = new JSONObject();
        json.put("id", cft.getId());
        return Result.success(json);
    }

    public Object update(JSONObject param) {
        String id = param.getString("id").trim();
        String name = param.getString("name").trim();
        String type = param.getString("type").trim();
        ChargeFeeType c = jdbcSession.findOne(ChargeFeeType.class)
                .eq("name", name)
                .exe();
        if(Objects.nonNull(c) && !id.equals(c.getId())){
            return Result.error(214030, "收费科目名称已经存在!");
        }
        ChargeFeeType cft = Pool.get(ChargeFeeType.class, id);
        cft.setFeetype(Enums.getEnum(FeesType.class, type));
        cft.setName(name);
        jdbcSession.updateById(cft);
        return Result.success();
    }

    public Object delete(String id) {
        ChargeFeeType cft = Pool.get(ChargeFeeType.class, id);
        if(Objects.isNull(cft)){
            Result.error(214031, "收费科目不存在!");
        }
        jdbcSession.deleteById(cft);
        return Result.success();
    }

    public Object get(String id) {
        ChargeFeeType cft = Pool.get(ChargeFeeType.class, id);
        if(Objects.isNull(cft)){
            Result.error(214031, "收费科目不存在!");
        }
        return Result.success(copyChargeFeeType(cft));
    }

    public Object list() {
        List<ChargeFeeType> list = jdbcSession.findArray(ChargeFeeType.class)
                .order("createTime", "DESC")
                .exe();
        List<ChargeFeeTypeExt> extList = new ArrayList<ChargeFeeTypeExt>();
        for(ChargeFeeType c : list){
            extList.add(copyChargeFeeType(c));
        }
        return Result.success(extList);
    }

    private ChargeFeeTypeExt copyChargeFeeType(ChargeFeeType cft){
        ChargeFeeTypeExt ext = new ChargeFeeTypeExt();
        ext.setId(cft.getId());
        ext.setName(cft.getName());
        if(Objects.nonNull(cft.getFeetype())){
            ext.setType(cft.getFeetype().getId());
        }
        ext.setCreateTime(cft.getCreateTime());
        return ext;
    }


}
